package kcms.message.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import kcms.db.Assignmentscore;
import kcms.db.Course;
import kcms.db.Message;
import kcms.db.Messagestatus;
import kcms.db.PptAndContent;
import kcms.db.Pptstatus;
import kcms.db.TeachStudent;
import kcms.db.ViewAssignScore;
import kcms.db.ViewMessage;
import kcms.message.dao.MessageDAO;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.type.BooleanType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class MessageDAOImpl extends HibernateDaoSupport implements MessageDAO{
	@Resource
	private SessionFactory sessionFactory;
	@Resource
	private Assignmentscore as;
	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	public Assignmentscore getAs() {
		return as;
	}

	public void setAs(Assignmentscore as) {
		this.as = as;
	}

	public List<TeachStudent> getAllTeach(String tid) {
		// TODO Auto-generated method stub
		String queryString="from TeachStudent where teacher=?";
		return (List<TeachStudent>)this.getHibernateTemplate().find(queryString,tid);
	}

	public boolean addMessage(Message m) {
		// TODO Auto-generated method stub
		try{
			this.getHibernateTemplate().save(m);
	    	return true;
	    	}
	    	catch(Exception ex){
	    		ex.printStackTrace();
	    		return false;
	    	}
	}

	public List<Message> getHisMessage(String tid) {
		// TODO Auto-generated method stub
		String hql="from Message where sender=?";
		return (List<Message>) this.getHibernateTemplate().find(hql, tid);
	}

	public Course getOneCourse(String cid) {
		// TODO Auto-generated method stub
		String hql="from Course where cid=?";
		return (Course)this.getHibernateTemplate().get(Course.class, cid);
	}

	public List<Course> getAllCourse() {
		String hql="from Course";
		return (List<Course>)this.getHibernateTemplate().find(hql);
	}
	
	public Message getOneMessage(int messageid){
		return (Message)this.getHibernateTemplate().get(Message.class,messageid);
	}

	public void deleteMessage(int messageid) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(this.getOneMessage(messageid));
	}

	public List<Messagestatus> getHisMessagestatus(String sid) {
		// TODO Auto-generated method stub
		String hql="from Messagestatus where sid=?";
		return (List<Messagestatus>)this.getHibernateTemplate().find(hql, sid);
	}
    
	public List<ViewMessage> getAllMessagesBySid(){
     String hql="from ViewMessage";
		return (List<ViewMessage>)this.getHibernateTemplate().find(hql);
	}
	
	public List<ViewMessage> getAllMessagesByCondition(String condition){
		if(condition.equals("1")||condition.equals("0"))
		{String hql="from ViewMessage  where status=?";
		  return (List<ViewMessage>)this.getHibernateTemplate().find(hql,Integer.parseInt(condition));}
		else{
	  String hql="from ViewMessage  order by "+condition;
	  return (List<ViewMessage>)this.getHibernateTemplate().find(hql);}
	}
	
	@SuppressWarnings("unchecked")
	//分页的查询
	public List<ViewMessage> getForPage(int offset, int length,String condition,String uid) {
		// TODO Auto-generated method stub
		Session session = this.sessionFactory.openSession();
		Query q = null;
		//查询条件为已读或者未读的消息
		//status  全部查询  但是未读的放在前面
		if(condition.equals("status"))
			{String hql="from ViewMessage where sid=? ORDER BY status desc";
			q = session.createQuery(hql);
			q.setParameter(0, uid);}
		
		//判断已读未读
		else if(condition.equals("1")||condition.equals("0"))
		{
		String hql="from ViewMessage  where status=? and sid=?";
		q = session.createQuery(hql);
		//如果未读
		if(condition.equals("1"))
		{q.setParameter(0,true);q.setParameter(1, uid);}
		else
		{q.setParameter(0,false);q.setParameter(1, uid);}	
		}else{
			String hql="from ViewMessage  where sender=? order by messageid desc";
			q = session.createQuery(hql);
			q.setParameter(0, uid);}
		q.setFirstResult(offset);
		q.setMaxResults(length);
		List<ViewMessage> list = q.list();
		session.close();
		return list;
	}

	public int getAllRowCount(String condition,String uid) {
		Session session = this.sessionFactory.openSession();
		if(condition.equals("status"))
		{
			String hql="from ViewMessage where sid=? ORDER BY status desc";
		    return this.getHibernateTemplate().find(hql,uid).size();
		    }
		else if(condition.equals("1")||condition.equals("0"))
		{
			String hql="from ViewMessage  where status=? and sid=?";
		if(condition.equals("1"))
			{
			Query q = session.createQuery(hql);
			q.setParameter(0,true);
			q.setParameter(1, uid);
		    List<ViewMessage> list=q.list();
		    return (int)list.size();
		    }
		  else
			{
			  Query q = session.createQuery(hql);
			  //q.setParameters(new Object [] { false,uid},new Type [] { new BooleanType(),new StringType() });
			  q.setParameter(0,false);q.setParameter(1, uid);
			  List<ViewMessage> list=q.list();
			  return (int)list.size();}
		}else
		{String hql="from Message  where sender=? order by messageid desc";
		return this.getHibernateTemplate().find(hql,uid).size();}
	
	}

	public void updateMessageStatus(Messagestatus messageStatus) {
		// TODO Auto-generated method stub	
				this.getHibernateTemplate().saveOrUpdate(messageStatus);
				}

	public Messagestatus getOneMessageStatusById(int messageid,String sid) {
		// TODO Auto-generated method stub
		String hql="from Messagestatus where messageid=? and sid=?";
		Session session = this.sessionFactory.openSession();
		Query q = session.createQuery(hql);
		q.setParameters(new Object [] { messageid,sid},new Type [] { new IntegerType(),new StringType() });
		return (Messagestatus)q.uniqueResult();
	}

	public ViewMessage getOneVMessage(int messageid,String sid) {
		// TODO Auto-generated method stub
		String hql="from ViewMessage where messageid=? and sid=?";
		Session session = this.sessionFactory.openSession();
		Query q = session.createQuery(hql);
		q.setParameters(new Object [] { messageid,sid},new Type [] { new IntegerType(),new StringType() });
		return (ViewMessage)q.uniqueResult();	}

	public List<ViewMessage> getAllMessagesBySender(String sender) {
		// TODO Auto-generated method stub
		String hql="from ViewMessage where  sender=?";
		return (List<ViewMessage>)this.getHibernateTemplate().find(hql, sender);
	}

	public void updateMessage(Message m) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().saveOrUpdate(m);;;
	}

	public void updateMessagestatus(Messagestatus ms) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(ms);
	}

	public List<Messagestatus> getMessagestatusById(int messageid) {
		// TODO Auto-generated method stub
		String hql="from Messagestatus where messageid=?";
		return (List<Messagestatus>) this.getHibernateTemplate().find(hql, messageid);
	}

	@Override
	public List<PptAndContent> listPptMessages(String sid) {
		// TODO Auto-generated method stub
		String hql="from PptAndContent where sid=?";
		return (List<PptAndContent>) this.getHibernateTemplate().find(hql,sid);
	}

	@Override
	public List<ViewAssignScore> listAssignMessages(String condition,String sid) {
		String hql="from ViewAssignScore v where v.sid=? ";
				if(condition.equals("0"))
					hql+="and v.status='0'";
				if(condition.equals("1"))
					hql+="and v.status='1'";
				return  (List<ViewAssignScore>)this.getHibernateTemplate().find(hql,sid);
	}

	@Override
	public ViewAssignScore getOneAssignMessage(int assignmentid) {
		// TODO Auto-generated method stub
		String hql="from ViewAssignScore  where assignmentid=?";
		List<ViewAssignScore> list=(List<ViewAssignScore>) this.getHibernateTemplate().find(hql, assignmentid);
		return list.get(0);
	}

	@Override
	public void updateAssignMessage(ViewAssignScore va) {
		// TODO Auto-generated method stub
		as.setAssignmentid(va.getAssignmentid());
		as.setScore(va.getScore());
		as.setPingyu(va.getPingyu());
		as.setStatus(va.getStatus());
		this.getHibernateTemplate().update(as);
	}
}
